package cn.jason.org.controller;

import cn.jason.basic.util.AjaxResult;
import cn.jason.basic.util.PageList;
import cn.jason.org.domain.DictionaryWord;
import cn.jason.org.query.DictionaryWordQuery;
import cn.jason.org.service.IDictionaryWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xxx
 */
@RestController
@RequestMapping("/dictionary/word")
@Api(value = "字典文字的API",description="字典文字相关的CRUD功能")
public class DictionaryWordController {

    private final IDictionaryWordService dictionaryWordService;

    public DictionaryWordController(IDictionaryWordService dictionaryWordService) {
        this.dictionaryWordService = dictionaryWordService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public DictionaryWord queryById(@PathVariable("id") Long id){

        return dictionaryWordService.queryById(id);
    }

    /**
     * 查询所有
     * @return List<Dictionary>
     */
    @GetMapping
    public List<DictionaryWord> queryAll(){
        return dictionaryWordService.queryAll();
    }

    /**
     * 高级查询
     * @param query 查询对象
     * @return PageList<Dictionary>
     */
    @PostMapping
    public PageList<DictionaryWord> queryPage(@RequestBody DictionaryWordQuery query){
        return dictionaryWordService.queryPage(query);
    }

    /**
     * 新增or修改
     * @param dictionaryWord  实体对象
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryWord dictionaryWord){
        try {
            if(dictionaryWord.getId() == null){
                dictionaryWordService.save(dictionaryWord);
            }else{
                dictionaryWordService.update(dictionaryWord);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("正在准备开服前的测试！！");
        }
    }

    /**
     * 删除
     * @param id id
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryWordService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("正在准备开服前的测试！！");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryWordService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("正在准备开服前的测试！！");
        }
    }


}
